<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2020/06/09 17:02
 * Desc: 订单退订
 */
class Controller_Pc_Api_V2_Refund extends Controller_Pc_Api_v2_Base
{
    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Refund
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Refund
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('refund', $this->request_params);

        // 服务
        $this->service = new Service_Api_Refund();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 获取退款前置信息
    public function action_check_refund()
    {
        try {
            $this->validation->check_refund()->check();

            $this->result['data'] = $this->service->check_refund($this->mid, $this->request_params['order_id']);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }


    // 获取退款详情
    public function action_get_info()
    {
        try {
            $this->validation->get_info()->check();

            $this->result['data'] = $this->service->get_info($this->mid, $this->request_params['order_id']);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 订单退订申请
    public function action_refund_apply()
    {
        try {
            $this->validation->refund_apply()->check();

            $this->result['data'] = $this->service->refund_apply($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    /**
     * 取消申请退款
     */
    public function action_cancel_refund_apply()
    {
        $db = Database::instance();
        $db->begin();
        try {
            $this->validation->cancel_refund_apply()->check();

            $this->result['msg'] = $this->service->cancel_refund_apply($this->mid, $this->request_params['order_id']);
            $this->result['data'] = array();

            $db->commit();
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());

            $db->rollback();
        }
    }

}